
package com.sawgdev.android.bokuiki.activity;

import com.sawgdev.android.bokuiki.R;
import com.sawgdev.android.bokuiki.bean.BeanHistory;
import com.sawgdev.android.bokuiki.common.Constants.EnumStatus;
import com.sawgdev.android.bokuiki.common.database.BKDBHelper;
import com.sawgdev.android.bokuiki.common.database.BkpfDao;
import com.sawgdev.android.bokuiki.common.database.IBkpfDao;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class ActHistory extends Activity {

    private ListView listHistory;

    private Button buttonBack;

    private View.OnClickListener clickButtonBack = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            finish();
        }
    };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.history);
    }

    @Override
    protected void onResume() {
        super.onResume();

        BKDBHelper helper = BKDBHelper.getInstance(this);
        IBkpfDao dao = new BkpfDao(helper.getReadableDatabase());

        List<HashMap<String, String>> arrayList = new ArrayList<HashMap<String, String>>();

        List<BeanHistory> historyList = dao.selTracks();
        if (historyList != null) {
            for (BeanHistory beanHistory : historyList) {
                HashMap<String, String> hashMap = new HashMap<String, String>();
                hashMap.put("date", beanHistory.getCreateDate());
                hashMap.put("location", beanHistory.getLocation());
                hashMap.put("status", EnumStatus.getMes(beanHistory.getStatus()));
                arrayList.add(hashMap);
            }

            SimpleAdapter simpleAdapter = new SimpleAdapter(this, arrayList, R.layout.list,
                    new String[] {
                            "date", "location", "status"
                    }, new int[] {
                            R.id.text_date, R.id.text_location, R.id.text_status
                    });
            listHistory = (ListView) findViewById(R.id.list_history);
            listHistory.setAdapter(simpleAdapter);
        }

        buttonBack = (Button) findViewById(R.id.button_back);
        buttonBack.setOnClickListener(clickButtonBack);
    }
}
